package main
import (
	"fmt"
	"github.com/garyburd/redigo/redis"
)
//定义全局变量连接池
var pool *redis.Pool

//初始化redis连接池
func init(){
	pool = &redis.Pool{
		MaxIdle: 8, //最大空闲连接数
		MaxActive: 0, //表示和数据库的最大连接数，0 表示没有限制
		IdleTimeout: 100, //最大空闲时间
		Dial:func() (redis.Conn,error){
			return redis.Dial("tcp","localhost:6379")
		},
	}
}

func main(){
	//先从pool连接池中取一个连接
	conn := pool.Get()
	defer conn.Close()

	_,err := conn.Do("Set","name","汤姆猫！")
	if err != nil{
		fmt.Println("Do Error :",err)
	}
	val,err := redis.String(conn.Do("Get","name"))
	if err != nil{
		fmt.Println("Do Error :",err)
	}
	fmt.Println("name :",val)

	_,err = conn.Do("HSet","engineer","name","fmx")
	if err != nil{
		fmt.Println("Do Error :",err)
	} 
	val,err = redis.String(conn.Do("HGet","engineer","name"))
	fmt.Println("name :",val)
	// pool.Close() //关闭连接池
	// conn2 := pool.Get() //测试关闭连接池后在取取连接
	// defer conn2.Close()
	// fmt.Println("conn2 : ",conn2)
	// val,err = redis.String(conn2.Do("Get","name"))
	// if err != nil{
	// 	fmt.Println("Do Error :",err)
	// }
	// fmt.Println("name :",val)

}